package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Result;
import com.dongdongshop.pojo.seller.TbSeller;
import com.dongdongshop.service.seller.SellerService;
import com.dongdongshop.service.seller_jpa.SellerJpaService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("seller")
public class SellerController {
    @Reference
    private SellerService sellerService;
    @Reference
    private SellerJpaService sellerJpaService;
    private final Logger logger = LoggerFactory.getLogger(SellerController.class);

    @RequestMapping("login")
    public String login(String username,String password){
        //1.获取Subject
        Subject subject = SecurityUtils.getSubject();
        //2.封装用户数据
        UsernamePasswordToken token = new UsernamePasswordToken(username,password);
        try {
            subject.login(token);
        }catch (UnknownAccountException e){
            return "shoplogin";
        }catch (IncorrectCredentialsException e){
            return "shoplogin";
        }
        return "redirect:/dongdongshop/index";
    }
    @RequestMapping("toRegister")
    public String toRegister(){
        return "register";
    }
    //注册
    @RequestMapping("register")
    @ResponseBody
    public Result register(TbSeller tbSeller){
        try {
            /*int i = sellerService.register(tbSeller);*/
            sellerJpaService.register(tbSeller);
            return Result.ok();
        }catch (Exception e){
            logger.error("注册失败",e);
            return Result.fail();
        }
    }
}
